using System;
using System.Collections.Generic;
using Framework.Timer;
using Framework.UI;
using GameSystem;
using IcecreamView;
using MintAnimation;
using TMPro;
using UnityEngine;
using Random = UnityEngine.Random;

namespace View
{
    public class ViewLoadingPanel : IC_AbstractModule
    {

        private string[] _tipsContext = new string[]
                                        {
                                            "加尔，因为你的力量，灯塔的火种得以保留！",
                                            "它们还会继续，就如那鬣狗一般难缠。",
                                            "灯塔是我们唯一的希望，谁也不会放弃它",
                                            "人们沉醉在灯塔的庇护下，可谁曾想过，灯塔熄灭了。",
                                            "还好我们保留了火种，还好我们还能修复它们.....",
                                            "有朝一日，灯塔会再次照亮整个山峰，但愿它不要来得太晚。"
                                        };

        [BindUIPath("")]
        private MintAnimationComponent _animationComponent;

        [BindUIPath("BG")]
        private MintAnimationComponent _bgAnimationComponent;

        [BindUIPath("BG/Title")]
        private TextMeshProUGUI _title;

        [BindUIPath("BG/Tips")]
        private TextMeshProUGUI _tips;

        private float _inTime;
        private float _dTime;
        private float _outTime;

        private Action _onComplete;

        public override void OnOpenView(Dictionary<string, IC_ViewData> parameters)
        {
            var data = parameters[UIManager.UIMsg];
            this._inTime     = data.GetValue<float>(0);
            this._dTime      = data.GetValue<float>(1);
            this._outTime    = data.GetValue<float>(2);
            this._onComplete = data.GetValue<Action>(3);
            this._title.text = GameManager.Instance.GameRoom.GameRoomData.LevelTitle;
            this._title.gameObject.SetActive(GameManager.Instance.GameFsm.LastState == GameEnum.Home);
            this._tips.text = this._tipsContext[Random.Range(0, this._tipsContext.Length)];

            this._bgAnimationComponent.gameObject.SetActive(false);

            this._animationComponent.GetOptions().Duration   = this._inTime;
            this._animationComponent.GetOptions().IsReversal = false;
            this._animationComponent.OnComplete = () =>
            {
                this._bgAnimationComponent.gameObject.SetActive(true);
                this._bgAnimationComponent.GetComponent<CanvasGroup>().alpha = 1;
                GameUpdateMgr.Instance.CreateTimer(this._dTime, this.EndLoading);
            };
            this._animationComponent.Play();
        }

        private void EndLoading()
        {
            this._bgAnimationComponent.Play();
            this._animationComponent.GetOptions().Duration   = this._outTime;
            this._animationComponent.GetOptions().IsReversal = true;
            this._onComplete?.Invoke();
            this._animationComponent.OnComplete = () =>
            {
                this.ViewConnector.CloseView();
                
            };
            this._animationComponent.Play();
        }

    }
}